# $Id: scan.regex.tcl 18 2012-01-06 23:56:32Z nickoos85@gmail.com $

proc scan:regex { uid } {
	set stop 0
	set stop2 0
	catch {open $::DIR/data/viri.dat r} liste
	while {![eof $liste]} { 
		gets $liste verif
		set viri_name [lindex $verif 0]
		set viri_dettype [lindex $verif 1]
		set viri_action [lindex $verif 2]
		set viri_dobl [lindex $verif 3]
		set viri_regex [lindex $verif 4]
		set viri_reason [lrange $verif 5 end]
		switch -exact $viri_dettype {
			"1" { # CTCP VERSION }
			"2" { # PRIVMSG }
			"3" {
				if {[regexp $viri_regex $::nick($uid)]} {
					altera:sendRaw ":$::altera(uid) PRIVMSG $::altera(salon) :$::nick($uid) is infected with $viri_name"; puts "$::nick($uid) is infected with $viri_name"
					switch -exact $viri_action {
						"1" { 
							switch -exact $viri_dobl {
								"0" { # Nothing }
								"1" { 
									puts "BL DONE"
									catch {open $::DIR/data/blacklist.dat r} liste2
									while {![eof $liste2]} { 
										gets $liste2 verif2; 
										if {![string compare -nocase $::ip($uid) $verif2]} { 
											set stop2 "1"
										}
									}
									catch {close $liste2}; 
									if {$stop2=="1"} { 
									} else {
										set blacklist [open $::DIR/data/blacklist.dat a]; puts $blacklist [encoding convertto iso8859-1 $::ip($uid)]; close $blacklist
									}
								}
							}
						}							
						"2" { 
							switch -exact $viri_dobl {
								"0" { # Nothing }
								"1" { 
									puts "BL DONE"
									catch {open $::DIR/data/blacklist.dat r} liste2
									while {![eof $liste2]} { 
										gets $liste2 verif2; 
										if {![string compare -nocase $::ip($uid) $verif2]} { 
											set stop2 "1"
										}
									}
									catch {close $liste2}; 
									if {$stop2=="1"} { 
									} else {
										set blacklist [open $::DIR/data/blacklist.dat a]; puts $blacklist [encoding convertto iso8859-1 $::ip($uid)]; close $blacklist
									}
								}
							}
							altera:sendRaw ":$::altera(uid) KILL $::nick($uid) :$viri_reason" }
						"3" { 
							switch -exact $viri_dobl {
								"0" { # Nothing }
								"1" { 
									puts "BL DONE"
									catch {open $::DIR/data/blacklist.dat r} liste2
									while {![eof $liste2]} { 
										gets $liste2 verif2; 
										if {![string compare -nocase $::ip($uid) $verif2]} { 
											set stop2 "1"
										}
									}
									catch {close $liste2}; 
									if {$stop2=="1"} { 
									} else {
										set blacklist [open $::DIR/data/blacklist.dat a]; puts $blacklist [encoding convertto iso8859-1 $::ip($uid)]; close $blacklist
									}
								}
							}
							altera:sendRaw ":$::altera(uid) GLINE *@$::ip($uid) 1d :$viri_reason" }
					}
					set stop "1"; break
				}
			}
			"4" { 
				if {[regexp $viri_regex $::ident($uid)]} {
					altera:sendRaw ":$::altera(uid) PRIVMSG $::altera(salon) :$::nick($uid) is infected with $viri_name"; puts "$::nick($uid) is infected with $viri_name"
					switch -exact $viri_action {
						"1" { 
							switch -exact $viri_dobl {
								"0" { # Nothing }
								"1" { 
									puts "BL DONE"
									catch {open $::DIR/data/blacklist.dat r} liste2
									while {![eof $liste2]} { 
										gets $liste2 verif2; 
										if {![string compare -nocase $::ip($uid) $verif2]} { 
											set stop2 "1"
										}
									}
									catch {close $liste2}; 
									if {$stop2=="1"} { 
									} else {
										set blacklist [open $::DIR/data/blacklist.dat a]; puts $blacklist [encoding convertto iso8859-1 $::ip($uid)]; close $blacklist
									}
								}
							}
						}							
						"2" { altera:sendRaw ":$::altera(uid) KILL $::nick($uid) :$viri_reason" }
						"3" { altera:sendRaw ":$::altera(uid) GLINE *@$::ip($uid) 1d :$viri_reason" }
					}
					set stop "1"; break
				}
			}
			"5" { 
				if {[regexp $viri_regex $::realname($uid)]} {
					altera:sendRaw ":$::altera(uid) PRIVMSG $::altera(salon) :$::nick($uid) is infected with $viri_name"; puts "$::nick($uid) is infected with $viri_name"
					switch -exact $viri_action {
						"1" { 
							switch -exact $viri_dobl {
								"0" { # Nothing }
								"1" { # Blacklist }
							}
						}							
						"2" { altera:sendRaw ":$::altera(uid) KILL $::nick($uid) :$viri_reason" }
						"3" { altera:sendRaw ":$::altera(uid) GLINE *@$::ip($uid) 1d :$viri_reason" }
					}
					set stop "1"; break
				}
			}
			"6" { # CHANNEL }
			"7" { # CHANNEL MESSAGE }
			"8" { 
				set scan "$::nick($uid)!$::ident($uid)@$::host($uid):$::realname($uid)"
				if {[regexp $viri_regex $scan]} {
					altera:sendRaw ":$::altera(uid) PRIVMSG $::altera(salon) :$::nick($uid) is infected with $viri_name"; puts "$::nick($uid) is infected with $viri_name"
					switch -exact $viri_action {
						"1" { 
							switch -exact $viri_dobl {
								"0" { # Nothing }
								"1" { # Blacklist }
							}
						}							
						"2" { altera:sendRaw ":$::altera(uid) KILL $::nick($uid) :$viri_reason" }
						"3" { altera:sendRaw ":$::altera(uid) GLINE *@$::ip($uid) 1d :$viri_reason" }
					}
					set stop "1"; break
				}
			}
		}
	}
	catch {close $liste}
	if {$stop == "1"} { } else { scan:host $uid }
}


